package com.open.sample.ui;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.Button;

import androidx.recyclerview.widget.RecyclerView;

import com.open.leanback.widget.ArrayObjectAdapter;
import com.open.leanback.widget.HeaderItem;
import com.open.leanback.widget.ItemBridgeAdapter;
import com.open.leanback.widget.ListRow;
import com.open.leanback.widget.OnChildViewHolderSelectedListener;
import com.open.leanback.widget.Presenter;
import com.open.leanback.widget.VerticalGridView;
import com.open.sample.R;
import com.open.sample.model.Movie;
import com.open.sample.model.MovieList;
import com.open.sample.presenter.CardPresenter;
import com.open.sample.presenter.NewPresenterSelector;

import java.util.Collections;
import java.util.List;

/**
 * VerticalActivity
 *
 * @author Clendy
 * @time 2016/11/20 16:13
 * @e-mail yc330483161@outlook.com
 */
public class VerticalActivity extends Activity {

    private static final String TAG = "hailongqiu";

    VerticalGridView mRecyclerView;
    ArrayObjectAdapter mRowsAdapter;
    ItemBridgeAdapter mItemBridgeAdapter;

    private static final int NUM_ROWS = 10;
    private static final int NUM_COLS = 5;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_vertical);
        mRecyclerView = findViewById(R.id.recyclerView);

        List<Movie> list = MovieList.setupMovies();
        final NewPresenterSelector newPresenterSelector = new NewPresenterSelector();
        // 填入Presenter选择器.
        mRowsAdapter = new ArrayObjectAdapter(newPresenterSelector);
        CardPresenter cardPresenter = new CardPresenter();

        int i;
        // 电影海报测试数据.
        for (i = 0; i < NUM_ROWS; i++) {
            if (i != 0) {
                Collections.shuffle(list);
            }
            ArrayObjectAdapter listRowAdapter = new ArrayObjectAdapter(cardPresenter);
            for (int j = 0; j < NUM_COLS; j++) {
                listRowAdapter.add(list.get(j % 5));
            }
            HeaderItem header = new HeaderItem(i, MovieList.MOVIE_CATEGORY[i % 5]);
            ListRow listRow = new ListRow(header, listRowAdapter);
            mRowsAdapter.add(listRow);
        }

        mItemBridgeAdapter = new ItemBridgeAdapter(mRowsAdapter);

        mRecyclerView.setAdapter(mItemBridgeAdapter);

        // 不然有一些item放大被挡住了. (注意)
        mRecyclerView.setClipChildren(false);
        mRecyclerView.setClipToPadding(false);
        // 设置间隔.
        mRecyclerView.setPadding(30, 30, 30, 30);
        // 设置垂直item的间隔.
        mRecyclerView.setVerticalMargin(30);
        // 设置缓存.
        mRecyclerView.getRecycledViewPool().setMaxRecycledViews(0, 100);

    }
}
